<?php

namespace App\Models;

use App\Services\Product\ExcelService;
use Carbon\Carbon;
use Illuminate\Database\Eloquent\Model;

class OrderStatement extends Model{

    //订单报表
    protected $table = 'order_statements';

    public $timestamps = false;

    protected $fillable = [
        //订单编号
        'order_num',
        //买家会员名
        'buyer_member_name',
        //买家支付宝账号
        'buyer_alipay',
        //买家应付货款
        'buyer_due_money',
        //买家应付邮费
        'buyer_due_postage',
        //买家支付积分
        'buyer_pay_point',
        //总金额
        'total_money',
        //返点积分
        'rebate_point',
        //买家实际支付金额
        'buyer_actually_pay_money',
        //买家实际支付积分
        'buyer_actually_pay_point',
        //订单状态
        'order_status',
        //买家留言
        'buyer_leave_word',
        //收货人姓名
        'recipients_name',
        //收货人地址
        'recipients_address',
        //运送方式
        'transport_way',
        //联系电话
        'contact_phone',
        //联系手机
        'contact_mobile',
        //订单创建时间
        'order_created_at',
        //订单付款时间
        'order_paid_at',
        //宝贝标题
        'commodity_title',
        //宝贝种类
        'commodity_classification',
        //物流单号
        'logistics_num',
        //物流公司
        'logistics_company',
        //订单备注
        'order_comment',
        //宝贝总数量
        'commodity_total_quantity',
        //店铺 id
        'shop_id',
        //店铺名称
        'shop_name',
        //订单关闭原因
        'order_closure_reason',
        //卖家服务费
        'seller_service_charge',
        //买家服务费
        'buyer_service_charge',
        //发票抬头
        'invoice_title',
        //是否手机订单
        'is_mobile_order',
        //分阶段订单信息
        'grading_order_message',
        //特权订金订单id
        'prerogative_subscription_order_id',
        //定金排名
        'subscription_ranking',
        //修改后的sku
        'modified_sku',
        //修改后的收货地址
        'modified_receiver_address',
        //异常信息
        'exception_message',
        //天猫卡券抵扣
        'tmall_card_deduction',
        //集分宝抵扣
        'jifenbao_deduction',
        //是否是O2O交易
        'is_o2o_trade',
    ];

    public function setOrderCreatedAtAttribute($value){
        $value = (new ExcelService)->setColumnValueToDateString($value);
        $this->attributes['order_created_at'] = $value;
    }

    public function setOrderPaidAtAttribute($value){
        $value = (new ExcelService)->setColumnValueToDateString($value);
        $this->attributes['order_paid_at'] = $value;
    }

    public function setContactPhoneAttribute($value){
        if(strpos($value, '\'') == 0){
            $value = substr($value, 1);
        }
        $this->attributes['contact_phone'] = $value;
    }

    public function setContactMobileAttribute($value){
        if(strpos($value, '\'') == 0){
            $value = substr($value, 1);
        }
        $this->attributes['contact_mobile'] = $value;
    }
    
}
